package com.lvchangqi.util;

import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.NicelyResynchronizingAjaxController;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.*;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by version on 17-6-18.
 */
@Component
public class Cet {

    public Map<String, Object> instance(String zkzh, String xm) throws IOException {
        WebClient webClient = new WebClient(BrowserVersion.CHROME);
        webClient.getOptions().setJavaScriptEnabled(true);
        webClient.getOptions().setCssEnabled(false);
        webClient.getOptions().setThrowExceptionOnScriptError(false);
        webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
        webClient.setAjaxController(new NicelyResynchronizingAjaxController());
        HtmlPage page = webClient.getPage(new URL("http://www.chsi.com.cn/cet/"));

        HtmlForm form = page.getFormByName("form1");
        form.getInputByName("zkzh").setValueAttribute(zkzh);//"420630162108726"
        form.getInputByName("xm").setValueAttribute(xm);//"王斯人"

        final HtmlPage resultPage = page.getHtmlElementById("submitCET").click();
        final HtmlDivision div = (HtmlDivision) resultPage.getElementById("leftH");
        final DomNodeList<HtmlElement> tbodys = div.getElementsByTagName("tbody");

        Map<String, Object> one = new HashMap<>();
        one.put("title", div.getElementsByTagName("h2").get(0).getTextContent().trim());
        HtmlTableBody tbody = null;

        if (tbodys.size() != 0) {
            tbody = (HtmlTableBody) tbodys.get(0);
            one.put("code", "200");
        }

        if(tbody != null){
            List<HtmlTableRow> rows = tbody.getRows();

            one.put("name", rows.get(0).getLastElementChild().getTextContent().trim());
            one.put("school", rows.get(1).getLastElementChild().getTextContent().trim());
            one.put("level", rows.get(2).getLastElementChild().getTextContent().trim());
            Map<String, String> two = new HashMap<>();
            two.put("admission", rows.get(4).getLastElementChild().getTextContent());
            two.put("score", rows.get(5).getLastElementChild().getTextContent().trim());
            two.put("listen", rows.get(6).getLastElementChild().getTextContent().trim());
            two.put("read", rows.get(7).getLastElementChild().getTextContent().trim());
            two.put("write", rows.get(8).getLastElementChild().getTextContent().trim());
            Map<String, String> three = new HashMap<>();
            three.put("admission", rows.get(10).getLastElementChild().getTextContent().trim());
            three.put("level", rows.get(11).getLastElementChild().getTextContent().trim());

            one.put("written", two);
            one.put("oral", three);
        } else {
            one.put("code", "无法找到对应的分数 请确认你输入的准考证号及姓名无误");
        }

        return one;
    }
}
